<?php

namespace app\admin\controller;

use think\Db;

class Article extends Base
{

    /**
     *获取咨讯分类
     */
    public function getArticleCate()
    {
        $getArticleCate = Db::name('article_cate')->paginate($this->pageSize, false, ['page' => $this->pageId]);
        $getArticleCate ? $this->suc($getArticleCate) : $this->err('获取咨讯分类失败');
    }

    /**
     *咨讯分类是否上架
     */
    public function changeArticleStatus()
    {
        $data = [];
        $data['id'] = input('id');
        $data['status'] = input('status');

        if (empty($data['id'])) {
            $this->err('缺少主键');
        }
        $res = Db::name('article_cate')->update($data);
        if ($res == 0) {
            $this->err('修改失败');
        } else {
            $this->suc($res);
        }
    }

    /**
     *删除咨讯分类
     */
   public function delArticleStatus(){
       $res=0;
       $data = [];
       $data['id'] = input('id');
       $data['status'] = input('status');

       if (empty($data['id'])) {
           $this->err('缺少主键');
       }
       if($data['status']==0){
           $res = Db::name('article_cate')->where(['id'=>$data['id']])->delete();
       }

       if ($res == 0) {
           $this->err('修改失败');
       } else {
           $this->suc($res);
       }
   }

    /**
     *咨讯分类新增
     */
    public function articleCateAdd()
    {
        $data = [];
        $data['name'] = input('name');
        input('status') != null ? $data['status'] = input('status') : $this->err('缺少分类状态！');
        if (empty($data['name'])) {
            $this->err('缺少分类名称！');
        }
        $data['createTime'] = createTime();
        $res = Db::name('article_cate')->insert($data);
        if ($res == 1) {
            $this->suc($res);
        } else if ($res == 0) {
            $this->err('新增分类失败');
        }
    }

    /**
     *获取咨讯文章列表
     */
    public function getArticleList()
    {
        $where = [];
        if (input('title')) {
            $where['a.title'] = ['like', '%' . input('title') . '%'];
        }
        if (input('categoryId')) {
            $where['a.categoryId'] = input('categoryId');
        }
        if (input('status') != null) {
            array_push($where, ['a.status', '=', input('status')]);
        }

        $getArticleList = Db::name('article')
            ->alias('a')
            ->join('article_cate c', 'a.categoryId=c.id', 'left')
            ->field('a.*,c.name')
            ->where($where)
            ->paginate($this->pageSize, false, ['page' => $this->pageId]);
        $getArticleList ? $this->suc($getArticleList) : $this->err('获取咨讯文章失败');
    }

    /**
     *删除咨讯文章
     */
    public function delArticle()
    {
        $data = [];
        $data['id'] = input('id');
        $data['uid'] = input('uid') ? input('uid') : 1;

        if (empty($data['id'])) {
            $this->err('缺少文章主键！');
        }

        $res = Db::name('article')->where('id', '=', $data['id'])->delete();
        $res ? $this->suc($res) : $this->err('删除文章出错！');
    }

    public function getArticleDetail()
    {
        $where = [];
        $where['id'] = input('id');
        if (empty($where['id'])) {
            $this->err('缺少文章主键！');
        }
        $res = Db::name('article')->where('id', '=', $where['id'])->find();
        $res ? $this->suc($res) : $this->err('获取文章详情出错！');
    }

    /**
     * 新增或者修改文章详情
     */
    public function editArticle()
    {
        $data = [];
        if (input('title') == null) {
            $this->err('文章标题不能为空');
        }

        input('publisher') ? $data['publisher'] = input('publisher') : $this->err('缺少发布人');

        if (input('sort') == null) {
            $this->err('排序不能为空');
        }
        if (input('categoryId') == null) {
            $this->err('分类编号不能为空');
        }
        input('articleType') ? $data['articleType'] = input('articleType') : $this->err('缺少文章类型！');
        input('content') ? $data['content'] = input('content') : $this->err('缺少内容！');
        input('clickCount') ? $data['clickCount'] = input('clickCount') : true;
        input('commentCount') ? $data['commentCount'] = input('commentCount') : true;
        input('collectCount') ? $data['collectCount'] = input('collectCount') : true;
        // 判断文章类型
        if($data['articleType'] == 2){
            input('videoSrc') ? $data['videoSrc'] = input('videoSrc') : $this->err('缺少视频链接！');

            //七牛获取上传视频封面
            $data['videoPicSrc'] = $data['videoSrc'] . '?vframe/jpg/offset/5/w/640/h/360';
        }
        //判断状态
        if(input('status') != 1 && input('status') != 0){
            input('videoSrc') ? $data['videoSrc'] = input('videoSrc') : $this->err('状态有误！');
        } else {
            $data['status'] = input('status');
        }

        $data['clickCount'] = input('clickCount');
        $data['title'] = input('title');
        $data['sort'] = input('sort');
        $data['categoryId'] = input('categoryId');

        if (input('id')) {

            $data['id'] = input('id');
            $res = model('article')->editData($data, $data['id']);
        } else {

            $data['createTime'] = createTime();
            $res = model('article')->editData($data);
        }
        $res ? $this->suc('保存成功') : $this->err('保存失败');

    }
    /**
     * 文章评论取消或展示
     */
    public function showArticleComment()
    {
        $where = [];
        input('cid') ? $where['cid'] = input('cid') : $this->err('缺少用户主键');
        input('articleId') ? $where['articleId'] = input('articleId') : $this->err('缺少文章主键');
        input('commentShow') != null ? $data['commentShow'] = input('commentShow') : $this->err('缺少修改条件');

        if ($data['commentShow'] != 1 && $data['commentShow'] != 0) {
            $this->err('展示类型出错');
        } else {
            $res = model('user_article')->field('id,isComment')->where($where)->find();

            $res['isComment'] ==1 ? true : $this->err('该文章尚未被评论！');
            if ($data['commentShow'] == 1) {
                $e_res = [
                    'id' => $res['id'],
                    'commentShow' => 1
                ];
                $rep = model('user_article')->editData($e_res, $e_res['id']);
                $rep ? $this->suc('展示成功！') : $this->err('展示失败！');
            } else {
                $e_res = [
                    'id' => $res['id'],
                    'commentShow' => 0
                ];
                $rep = model('user_article')->editData($e_res, $e_res['id']);
                $rep ? $this->suc('隐藏成功！') : $this->err('隐藏失败！');
            }
        }

    }
    /**
     * 文章评论列表
     */
    public function getCommentArticleList()
    {

        $where = [];
        input('name') ? $where['name']=['like', '%' . input('name') . '%'] : true;
      /*  input('id') ? $where['articleId'] = input('id') : $this->err('缺少文章主键');
        $article = model('article')->where(['id' => $where['articleId']])->find();
        if (!$article) {
            $this->err('用户编码错误！');
        }*/

        $where['isComment'] = 1;

        $res = model('user_article')
            ->alias('ua')
            ->field('ua.*,u.name,u.avatar')
            ->join('user u', 'ua.cid=u.id', 'left')
            ->where($where)
            ->order('ua.id')
            ->paginate(10)
            ->toArray();

        $res ? $this->suc($res) : $this->err('获取评论列表失败');
    }

}
